package com.eat.fish_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eat.common_utils.IpAndPortUtil;
import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.BaikeImgs;
import com.eat.fish_service.entity.Dao.Diseaseimgmore;
import com.eat.fish_service.mapper.DiseaseimgmoreMapper;
import com.eat.fish_service.service.DiseaseimgmoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LWB
 * @since 2022-05-23
 */
@Service
@Slf4j
public class DiseaseimgmoreServiceImpl extends ServiceImpl<DiseaseimgmoreMapper, Diseaseimgmore> implements DiseaseimgmoreService {

    @Autowired
    private DiseaseimgmoreMapper diseaseimgmoreMapper;

    @Value("${uploadDetailDiseasePath}")
    private String uploadDetailDiseasePath;


    @Override
    public R getMoreDiseaseImg(String diseaseName) {

        ArrayList<String> diseaseimgmoreArrayList = new ArrayList<>();

        //构建条件
        QueryWrapper<Diseaseimgmore> wrapper = new QueryWrapper<>();

        List<Diseaseimgmore> diseaseimgmoreList = diseaseimgmoreMapper.selectList(wrapper);
        for (Diseaseimgmore diseaseimgmore : diseaseimgmoreList) {

            String diseaseImg = diseaseimgmore.getDiseaseImg();
            diseaseimgmoreArrayList.add(diseaseImg);
        }

        return R.ok().data("diseaseimgmoreArrayList", diseaseimgmoreArrayList);
    }

    //鱼病百科批量添加鱼病详细图片方法
    @Override
    public R uploadDetailDiseasePic(MultipartFile[] pic, String diseaseName) {
        if (pic != null) {
            log.info("图片张数："+pic.length);
            //存放传入鱼病详细图片的存放路径
            ArrayList<String> paths = new ArrayList<>();
            //第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录
            File file = new File(uploadDetailDiseasePath);
            if (!file.exists()) {
                file.mkdirs();
            }

            try {
                //取出每张鱼病图片，依次存入服务器
                for (int i = 0; i < pic.length; i++) {
                    String fileName = System.currentTimeMillis() + pic[i].getOriginalFilename();
                    //把浏览器上传的文件复制到希望的位置
                    File targetFile = new File(uploadDetailDiseasePath, fileName);
                    pic[i].transferTo(targetFile);
                    //服务器的ip和端口号
                    String ipAndPort = IpAndPortUtil.getIpAndPort();
                    String picUrl = "http://" + ipAndPort + "/getDetailDisease/" + fileName;
                    //将图片的url放入返回集合中
                    paths.add(picUrl);
                    log.info("第"+i+"张："+picUrl);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return R.error().message("上传失败！");
            }

            //将鱼病名称及鱼病详细图片的url存入数据库
            for (int i = 0; i < paths.size(); i++) {
                Diseaseimgmore diseaseimgmore = new Diseaseimgmore();
                diseaseimgmore.setDiseaseImg(paths.get(i));
                diseaseimgmore.setDiseaseName(diseaseName);
                baseMapper.insert(diseaseimgmore);
            }
            return R.ok();
        } else {
            return R.error().message("上传的文件为空！");
        }
    }

    @Override
    public R getAllFishPic(String diseaseName) {
        QueryWrapper<Diseaseimgmore> wrapper = new QueryWrapper<>();
        wrapper.eq("disease_name", diseaseName);
        List<Diseaseimgmore> list = baseMapper.selectList(wrapper);

        if (list == null){
            return R.error().message("该鱼类不存在！");
        }

        ArrayList<String> pic = new ArrayList<>();
        for (Diseaseimgmore bk : list) {
            pic.add(bk.getDiseaseImg());
        }

        return R.ok().data("pic", pic);
    }
}
